package com.itheima.manager;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.common.Code;
import com.itheima.common.PageResult;
import com.itheima.common.R;
import com.itheima.domain.PmsProductCategory;
import com.itheima.exception.BusinessException;
import com.itheima.service.PmsProductCategoryService;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PmsProductCategoryManager {

    @DubboReference
    PmsProductCategoryService pmsProductCategoryService;

    public PageResult pageByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        return pmsProductCategoryService.pageByParentId(parentId,pageNum,pageSize);

    }

    public PmsProductCategory selectById(Integer id) {
        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
        return productCategory;
    }

    public void create(PmsProductCategory pmsProductCategory) {
        pmsProductCategoryService.save(pmsProductCategory);
    }

    public void deleteById(Integer id) {
        pmsProductCategoryService.removeById(id);
    }

    public void updateById(PmsProductCategory productCategoryController) {
        pmsProductCategoryService.updateById(productCategoryController);
    }

    public void updateNavStatus(ArrayList<Integer> ids, Integer navStatus) {
        for (Integer id : ids) {
            PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
            productCategory.setNavStatus(navStatus);
            pmsProductCategoryService.updateById(productCategory);
        }
    }

    public void updateShowStatus(ArrayList<Integer> ids, Integer showStatus) {
        for (Integer id : ids) {
            PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
            productCategory.setShowStatus(showStatus);
            pmsProductCategoryService.updateById(productCategory);
        }
    }

//    public List<PmsProductCategoryWithChildrenItem> getWithChildren() {
//        List<PmsProductCategoryWithChildrenItem> list1 = pmsProductCategoryService.getWithChildren();
//        return list1;
//    }

    //查询所有一级分类及子分类
    public List<PmsProductCategoryVo> withChildren() {

        List<PmsProductCategory> categoryList = pmsProductCategoryService.findAll();

        if (ObjectUtils.isEmpty(categoryList)) throw new BusinessException("查询数据为空");

        List<PmsProductCategoryVo> resList = new ArrayList<>();

        for (PmsProductCategory category : categoryList) {
            PmsProductCategoryVo pmsProductCategoryVo = new PmsProductCategoryVo();
            BeanUtils.copyProperties(category, pmsProductCategoryVo);
            pmsProductCategoryVo.setChildren(new ArrayList<>());

            // 组装父级
            if (0 == category.getParentId()) {
                for (PmsProductCategory pmsProductCategory : categoryList) {
                    if (category.getId().equals(pmsProductCategory.getParentId())) {
                        pmsProductCategoryVo.getChildren().add(pmsProductCategory);
                    }
                }
                resList.add(pmsProductCategoryVo);
            }
        }
        return resList;
    }
}
